//
//  SLOriginalView.swift
//  Sina15
//
//  Created by 盼 on 16/4/13.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class SLOriginalView: UIView {
    
    //记录原创微博的底部约束
    var bottomCons:Constraint?
    
    //在模型中绑定数据
    var statusModel: SLStatusViewModel? {
        
        didSet {
            iconView.sd_setImageWithURL(statusModel?.iconURL)
            nameLabel.text = statusModel?.status?.user?.name

            mbRankImageView.image = statusModel?.mbImage
            
            //设置微博认证类型
            verifiedImageView.image = statusModel?.verifiedImage
            //微博正文
            contentLabel.text = statusModel?.status?.text
            
            //微博图片地址
            let picCount = statusModel?.status?.pic_urls?.count ?? 0
            
            //微博来源
            sourceLabel.text = statusModel?.source
            
            //微博时间
            timeLabel.text = statusModel?.created_at
            
            //更新约束之前要先卸载约束
            self.bottomCons?.uninstall()
            
            //没有配图视图
            if picCount == 0 {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //微博底部以微博正文为约束
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
                //模型数组为nil
                pictureView.pic_URLS = nil
            }else{
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //以配图视图底部为约束
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                })
                //配图视图不隐藏
                pictureView.hidden = false
                //给模型数组赋值
                pictureView.pic_URLS = statusModel?.status?.pic_urls
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
         setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //设置UI
    func setupUI(){
        
        //添加子控件
        addSubview(sepView)
        addSubview(iconView)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //添加图片视图
        addSubview(pictureView)
        
        //把头像裁剪成圆角
        iconView.cornerRadius = 20
        
        self.sepView.backgroundColor = UIColor.redColor()
        
        //设置约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(cellMargin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(cellMargin)
            make.left.equalTo(self).offset(cellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(cellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(cellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(iconView.snp_right).offset(cellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(cellMargin)
//            make.right.equalTo(self).offset(-8)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(cellMargin)
            make.left.equalTo(self).offset(cellMargin)
            make.right.equalTo(self).offset(-cellMargin)
        }
        
//        pictureView.backgroundColor = randomColor()
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            //设置图片距离左边的约束
            make.left.equalTo(contentLabel)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //自动计算原创视图的高度
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin)
         self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
        }
    }

    //用户头像
    private lazy var iconView: UIImageView = {
        
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        return icon
    }()
    
    //用户等级
    private lazy var mbRankImageView: UIImageView = {
        
        let rank = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
        
        return rank
    }()
    
    //认证类型
    private lazy var verifiedImageView: UIImageView = {
       
        let verified = UIImageView(image: UIImage(named: "avatar_vip"))
        
        return verified
    }()
    
    //微博用户名
    private lazy var nameLabel = UILabel(labelText: "susie", color: UIColor.darkGrayColor(), fontsize: 14)
    
    //微博创建时间
    private lazy var timeLabel = UILabel(labelText: "13:00", color: UIColor.darkGrayColor(), fontsize: 14)
    
    //微博来源
    private lazy var sourceLabel = UILabel(labelText: "我就是原创！", color: UIColor.darkGrayColor(), fontsize: 14,margin: 8)
    
    //微博正文
    private lazy var contentLabel = UILabel(labelText: "未来有一场旅行", color: UIColor.darkGrayColor(), fontsize: 14,margin: 8)
    
    //设置分割线
    private lazy var sepView: UIView = UIView()
    
    //配图视图
    private lazy var pictureView: SLStatusPictureView = SLStatusPictureView()
}
